package com.skstudio.report;

import java.util.ArrayList;
import java.util.HashMap;

public class ReportThreads extends ReportBase {

    public static class DataThread {

        public long mTid, mState, mCpuTime;
        public float mPctUser, mPctKernel;

        public void Read(byte[] buffer, int offset) {
            mTid = getCxxUnsignedLong(buffer, offset + 0);
            mState = getCxxUnsignedLong(buffer, offset + 4);
            float value = getCxxUnsignedLong(buffer, offset + 8);
            mPctUser = value / 100.0f;
            value = getCxxUnsignedLong(buffer, offset + 12);
            mPctKernel = value / 100.0f;
            mCpuTime = getCxxUnsignedLong(buffer, offset + 16);
        }
    }

    public static class ThreadClass {

        public int mStartedFrom = 0;
        public ArrayList<DataThread> mThreadDataQueue = new ArrayList<DataThread>();

        public boolean isDead() {
            if (mThreadDataQueue.size() + mStartedFrom < ReportBase.mRecordCount) {
                return true;
            }
            return false;
        }

        public long getTid() {
            if (mThreadDataQueue.isEmpty()) return 0;
            return mThreadDataQueue.get(0).mTid;
        }

        public float getThreadCpuUsage(int index) {
            return mThreadDataQueue.get(index).mPctUser + mThreadDataQueue.get(index).mPctKernel;
        }

        public long getCpuTime(int index) {
            return mThreadDataQueue.get(index).mCpuTime;
        }
    }
    
    public ArrayList<ThreadClass> mDataList = new ArrayList<ThreadClass>();
    public HashMap<Long, String> mNameMap = new HashMap<Long, String>();

    @Override
    public void parse(byte[] buffer, int offset) {
        DataThread dc = new DataThread();
        dc.Read(buffer, offset);

        boolean fMatch = false;
        ThreadClass qdc = null;
        for (int ii = 0; ii < mDataList.size(); ii++) {
            if (mDataList.get(ii).mThreadDataQueue.get(0).mTid == dc.mTid) {
                fMatch = true;
                qdc = mDataList.get(ii);
                break;
            }
        }
        if (!fMatch) {
            ThreadClass threadLine = new ThreadClass();
            threadLine.mStartedFrom = ReportBase.mRecordCount;
            mDataList.add(threadLine);
            qdc = mDataList.get(mDataList.size() - 1);
        }

        if (qdc.mThreadDataQueue.size() == getMaxQueueSize()) {
            qdc.mThreadDataQueue.remove(0);
        }
        qdc.mThreadDataQueue.add(dc);
    }

    public String getThreadName(long tid) {
        Long id = new Long(tid);
        return mNameMap.get(id);
    }

    public void setThreadName(long tid, String name) {
        Long id = new Long(tid);
        mNameMap.put(id, name);
    }

    public boolean isThreadDead(long tid) {
        ThreadClass qdc = null;
        for (int ii = 0; ii < mDataList.size(); ii++) {
            if (mDataList.get(ii).mThreadDataQueue.get(0).mTid == tid) {
                qdc = mDataList.get(ii);
                return qdc.isDead();
            }
        }
        return true;
    }
}
